<template>
	<view>
		<uni-nav-bar :border="false" left-icon="left" :title="title" @clickLeft="back">
			<block v-slot:right>
				<navigator :url="`/pages/reminder-editor/reminder-editor?activityId=${activityId}&activityName=${activityName}&edit=false`">
					<button type="primary" plain="true" size="mini">添加</button>
				</navigator>
			</block>
		</uni-nav-bar>

		<uni-list>
			<uni-list-item
				v-for="item in reminderHistory"
				:title="item.content"
				:rightText="`${item.time}`"
				link
				:to="`/pages/reminder-editor/reminder-editor?activityId=${activityId}&activityName=${activityName}&edit=true&content=${item.content}&time=${item.time}&id=${item.id}`"
			></uni-list-item>
		</uni-list>

		<uni-load-more iconType="circle" :status="status" @clickLoadMore="getMoreReminder" />
	</view>
</template>

<script>
export default {
	data() {
		return {
			title: '',
			status: 'more',
			pageNum: 1,
			pageSize: 5,
			reminderHistory: [],
			activityId: 1,
			activityName: '',
			apiUrl: 'http://localhost:8080/'
		};
	},
	onPullDownRefresh() {
		this.pageNum = 1;
		this.getReminderHistory();
	},
	onShow() {
		// 添加活动后返回到列表页,需要刷新进行显示
		uni.startPullDownRefresh();
	},
	onLoad: function(option) {
		this.activityId = option.activityId;
		this.activityName = option.activityName;

		this.title = option.activityName;
		this.getReminderHistory();
	},
	methods: {
		back() {
			uni.navigateBack({
				delta: 1
			});
		},
		getMoreReminder() {
			if (this.status == 'more') {
				this.pageNum++;
				this.getReminderHistory();
			}
		},
		getReminderHistory() {
			uni.request({
				method: 'GET',
				url: this.apiUrl + 'reminder/' + this.activityId,
				header: {
					// Authorization: uni.getStorageSync('token')
				},
				data: {
					pageNum: this.pageNum,
					pageSize: this.pageSize
				},
				success: res => {
					console.log(res.data);
					if (this.pageNum <= 1) {
						this.reminderHistory = [];
					}
					let originSize = this.reminderHistory.length;
					this.reminderHistory.push.apply(this.reminderHistory, res.data.records);
					let deltaSize = res.data.records.length;
					if (res.data.total <= originSize + deltaSize) {
						this.status = 'noMore';
					} else {
						this.status = 'more';
					}

					uni.stopPullDownRefresh();
				},
				fail: err => {
					this.pageNum -= 1;
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
		}
	}
};
</script>

<style lang="scss">
$uni-success: #18bc37 !default;
/* 修改buttom默认样式 */
button {
	font-size: 20rpx;
}

.decoration {
	width: 6px;
	height: 6px;
	margin-right: 4px;
	border-radius: 50%;
	background-color: $uni-success;
}
</style>
